import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: LIUYANJIAO
 * Date: 2025-03-10
 * Time: 20:39
 */
public class test3 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        String str1 = scanner.next();
        String str2 = scanner.next();
        String[] strs = new String[n];
        for (int i = 0; i < n; i++) {
            strs[i] = scanner.next();
        }
        int count1 = -1;
        int count2 = -1;
        for (int i = 0; i < n; i++) {
            if (strs[i].equals(str1)) {
                if (count1 == -1 || count2 == -1) {
                    count1 = i;
                } else if (Math.abs(count1 - count2) > Math.abs(i - count2)) {
                    count1=i;
                }
            }
            if (strs[i].equals(str2)) {
                if (strs[i].equals(str2)) {
                    if (count1 == -1 || count2 == -1) {
                        count2 = i;
                    } else if (Math.abs(count1 - count2) > Math.abs(i - count2)) {
                        count2=i;
                    }
                }
            }

        }
        if (count1!= -1 && count2!= -1) {
            System.out.println(Math.abs(count1 - count2));
        }else {
            System.out.println(-1);
        }
    }
}
